//
//  MainViewController.m
//  项目一
//
//  Created by nick on 15/7/17.
//  Copyright (c) 2015年 nick. All rights reserved.
//

#import "MainViewController.h"
#import "movieViewController.h"
#import "moreViewController.h"
#import "cinemaViewController.h"
#import "topViewController.h"
#import "newsViewController.h"
#import "myButton.h"
#import "baseNavigationController.h"
@interface MainViewController (){
    UIImageView *_selectImageView;
}

@end

@implementation MainViewController
- (void)_createSubView{
    movieViewController *vc1 = [[movieViewController alloc]initWithTitle:@"电影"];
    newsViewController *vc2 = [[newsViewController alloc]initWithTitle:@"新闻"];
    topViewController *vc3 = [[topViewController alloc]initWithTitle:@"top"];
    cinemaViewController *vc4 = [[cinemaViewController alloc]initWithTitle:@"影院"];
    moreViewController *vc5 = [[moreViewController alloc]initWithTitle:@"更多"];
    NSArray *vcarray = @[vc1,vc2,vc3,vc4,vc5];
    NSMutableArray *nvcarray = [[NSMutableArray alloc]init];
    for (int i = 0; i < 5; i++) {
        baseNavigationController *nvc = [[baseNavigationController alloc]initWithRootViewController:vcarray[i]];
        [nvcarray addObject:nvc];
    }
    self.viewControllers = nvcarray;
}
- (void)_setTabBar{
    for (UIView *subView in self.tabBar.subviews) {
        Class cls = NSClassFromString(@"UITabBarButton");
        if ([subView isKindOfClass:cls]) {
            [subView removeFromSuperview];
        }
    }
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];
        CGFloat height = CGRectGetHeight(self.tabBar.frame);
        CGFloat width = CGRectGetWidth(self.tabBar.frame)/5;
        NSArray *titles = @[@"电影",@"新闻",@"top",@"影院",@"更多"];
        NSArray *images = @[@"movie_cinema@2x",@"msg_new@2x",@"start_top250@2x",@"icon_cinema@2x",@"more_setting@2x"];
    _selectImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    _selectImageView.image = [UIImage imageNamed:@"selectTabbar_bg_all1@2x.png"];
    [self.tabBar addSubview:_selectImageView];
        for (int i=0; i<5; i++) {
//            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i*width, 0, width, height)];
//            [button setTitle:titles[i] forState:UIControlStateNormal];
//            [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
//            [self.tabBar addSubview:button];
            myButton *btn = [[myButton  alloc]initWithFrame:CGRectMake(i*width, 0, width, height) withLable:titles[i] withImage:images[i]];
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = i;
            [self.tabBar addSubview:btn];
        }

    }
    
//    for (int i = 0;i<5 ; i++) {
//        UIButton *btn = []
//    }
- (void)btnAction:(myButton*)btn{
    self.selectedIndex = btn.tag;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:0.3];
    _selectImageView.center = btn.center;
    
    [UIView commitAnimations];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createSubView];
    [self _setTabBar];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
